package com.kaka.bus.engine.borrow.person.step;

import android.support.v7.widget.AppCompatButton;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.kaka.bus.R;
import com.kaka.bus.base.fragment.SimpleFragment;
import com.kaka.bus.constant.UserConstant;
import com.kaka.bus.engine.borrow.person.PersonActivity;
import com.kaka.bus.manager.BottomDialogManager;
import com.kaka.bus.util.U;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by zhangqi on 2017/12/21.
 * e-mail : ${email}
 * desc :
 */

public class Step2Fragment extends SimpleFragment {

    @BindView(R.id.tv_job_job)
    TextView mTvJobJob;
    @BindView(R.id.tv_job_money)
    TextView mTvJobMoney;
    @BindView(R.id.edt_job_gsname)
    EditText mEdtJobGsname;
    @BindView(R.id.tv_job_province)
    TextView mTvJobProvince;
    @BindView(R.id.tv_job_city)
    TextView mTvJobCity;
    @BindView(R.id.edt_job_gsaddress)
    EditText mEdtJobGsaddress;
    @BindView(R.id.edt_job_gsphone)
    EditText mEdtJobGsphone;
    @BindView(R.id.btn_job_commit)
    AppCompatButton mBtnJobCommit;
    @BindView(R.id.edt_post)
    EditText edt_post;
    private ArrayList<String> mJobList;
    private ArrayList<String> mMoneyList;
    private ArrayList<String> mProvinceList;
    private ArrayList<String> mCityList;

    private BottomDialogManager mJobManager;
    private BottomDialogManager mMoneyManager;
    private BottomDialogManager mProvinceManager;
    private BottomDialogManager mCityManager;

    private Map<String, ArrayList<String>> mCityMap;
    private PersonActivity mPersonActivity;
    private LinkedHashMap<String, String> mJobMap;

    protected void initView(View view) {
        mPersonActivity = (PersonActivity) getActivity();
        mJobManager = new BottomDialogManager(mActivity);
        mMoneyManager = new BottomDialogManager(mActivity);
        mProvinceManager = new BottomDialogManager(mActivity);
        mCityManager = new BottomDialogManager(mActivity);
    }

    @Override
    protected void initData() {
        mPersonActivity.getCity();

        /**
         * 加载缓存
         */
        if (mPersonActivity != null) {
            mTvJobJob.setText(mPersonActivity.mPMap.get(UserConstant.PROFESSION));
            mTvJobMoney.setText(mPersonActivity.mPMap.get(UserConstant.MONTHLYPAY));
            mEdtJobGsname.setText(mPersonActivity.mPMap.get(UserConstant.BUSINESS));
            mTvJobProvince.setText(mPersonActivity.mPMap.get(UserConstant.BUSI_PROVINCE));
            mTvJobCity.setText(mPersonActivity.mPMap.get(UserConstant.BUSI_CITY));
            mEdtJobGsaddress.setText(mPersonActivity.mPMap.get(UserConstant.BUSI_ADDRESS));
            mEdtJobGsphone.setText(mPersonActivity.mPMap.get(UserConstant.BUSI_PHONE));
        }

        /**
         * 创建集合添加参数
         */
        if (mJobMap == null) {
            mJobMap = new LinkedHashMap<>();
        }

        /**
         * 职业相关参数的初始化
         */
        if (mJobList == null) {
            mJobList = new ArrayList<>();
            mJobList.add("企、事业单位负责人");
            mJobList.add("专业技术人员");
            mJobList.add("办事人员和有关人员");
            mJobList.add("商业、服务业人员");
            mJobList.add("农、林、牧、鱼、水利业生产人员");
            mJobList.add("生产、运输设备操作人员及有关人员");
            mJobList.add("军人");
            mJobList.add("其他");
            mJobManager.setData(mJobList);
        }

        /**
         * 收入相关参数的初始化
         */
        if (mMoneyList == null) {
            mMoneyList = new ArrayList<>();
            mMoneyList.add("0~3000");
            mMoneyList.add("3000~6000");
            mMoneyList.add("6000~9000");
            mMoneyList.add("9000~12000");
            mMoneyList.add("12000~15000");
            mMoneyList.add("15000以上");
            mMoneyManager.setData(mMoneyList);
        }
    }

    @Override
    protected void initListener() {
        mTvJobJob.setOnClickListener(v -> mJobManager.show());
        mTvJobMoney.setOnClickListener(v -> mMoneyManager.show());
        mTvJobProvince.setOnClickListener(v -> mProvinceManager.show());

        mTvJobCity.setOnClickListener(v -> {
            String province = mTvJobProvince.getText().toString();

            if (TextUtils.isEmpty(province)) {
                showMessage("请先选择省份!");
            } else {
                mCityManager.show();
            }
        });

        mJobManager.setOkListener(select -> mTvJobJob.setText(select));
        mMoneyManager.setOkListener(select -> mTvJobMoney.setText(select));
        mProvinceManager.setOkListener(select -> {
            mTvJobProvince.setText(select);
            mTvJobCity.setText("");
            ArrayList<String> cityList = mCityMap.get(select);
            if (mCityManager.getList() == null) {
                mCityManager.setData(cityList);
            } else {
                mCityManager.refreshData(cityList);

            }
        });
        mCityManager.setOkListener(select -> mTvJobCity.setText(select));

        mBtnJobCommit.setOnClickListener(v -> {
            if (check(true)) {
                mPersonActivity.stepContinue(1);
            }
        });
    }

    public boolean check(boolean flag) {

        String job = getText(mTvJobJob);
        String money = getText(mTvJobMoney);
        String gsName = getText(mEdtJobGsname);
        String gsAddress = getText(mEdtJobGsaddress);
        String gsPhone = getText(mEdtJobGsphone).trim();
        String province = getText(mTvJobProvince);
        String city = getText(mTvJobCity);
        String post = getText(edt_post);
        if (flag) {
            if (TextUtils.isEmpty(job)) {
                showMessage("请选择职业!");
                return false;
            }
            if (TextUtils.isEmpty(job)) {
                showMessage("请填写职位");
                return false;
            }

            if (TextUtils.isEmpty(money)) {
                showMessage("请选择收入状况!");
                return false;
            }

            if (TextUtils.isEmpty(gsName)) {
                showMessage("请填写单位名称!");
                return false;
            }

            if (TextUtils.isEmpty(province)) {
                showMessage("请选择省份!");
                return false;
            }

            if (TextUtils.isEmpty(city)) {
                showMessage("请选择城市!");
                return false;
            }

            if (TextUtils.isEmpty(gsAddress)) {
                showMessage("请填写单位详细地址!");
                return false;
            }

            if (TextUtils.isEmpty(gsPhone)) {
                showMessage("请填写单位电话!");
                return false;
            }

            if (!U.isFixedPhone(gsPhone)) {
                showMessage("请填写正确的单位电话!");
                return false;
            }
        }

        if (mJobMap != null) {
            mJobMap.put(UserConstant.PROFESSION, job);
            mJobMap.put(UserConstant.BUSI_POST, post);
            mJobMap.put(UserConstant.MONTHLYPAY, money);
            mJobMap.put(UserConstant.BUSINESS, gsName);
            mJobMap.put(UserConstant.BUSI_PROVINCE, province);
            mJobMap.put(UserConstant.BUSI_CITY, city);
            mJobMap.put(UserConstant.BUSI_ADDRESS, gsAddress);
            mJobMap.put(UserConstant.BUSI_PHONE, gsPhone);
        }

        if (mPersonActivity != null) {
            mPersonActivity.mPMap.putAll(mJobMap);
        }
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_job;
    }

    public void setPro(Map<String, ArrayList<String>> cityMap, ArrayList<String> province) {
        mProvinceList = province;
        mCityMap = cityMap;

        if (mProvinceManager != null) {
            mProvinceManager.setData(mProvinceList);
        }
    }

}
